//
//  ConsultTableCell.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-29.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "ConsultTableCell.h"

//消息记录与背景间隔
#define ChatLeftGap             22
#define ChatRightGap            14
#define ChatYGap                10

//
#define MR_TX_NAME              @"登陆页-用户默认头像.png"

@interface ConsultTableCell()
{
    //咨询时间
    UILabel *timeLabel;
    
    //我的头像
    UIImageView *myImgaeView;
    
    //我的咨询
    UILabel *myLabel;
    //背景
    UIImageView *myLabelBg;
    
    //客服头像
    UIImageView *serviceImgaeView;
    
    //客服回答
    UILabel *answerLabel;
    //背景
    UIImageView *answerLabelBg;
}
@end

@implementation ConsultTableCell

//初始化
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier size:(CGSize)size
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        timeLabel = [[UILabel alloc] initWithFrame:CGRectMake((size.width-120)/2, 12, 120, 16)];
        [timeLabel setBackgroundColor:[z_UI colorWithHexString:@"f0f0f0"]];
        [timeLabel setTextColor:GET_COLOR(@"aaaaaa")];
        [timeLabel setFont:[UIFont boldSystemFontOfSize:10]];
        [timeLabel setTextAlignment:NSTextAlignmentCenter];
        [self.contentView addSubview:timeLabel];
        //设置圆角
        timeLabel.layer.masksToBounds = YES;
        timeLabel.layer.cornerRadius = 3;
        
        //我的
        myImgaeView = [[UIImageView alloc] initWithFrame:CGRectMake(size.width-14-37.5, timeLabel.frame.size.height+timeLabel.frame.origin.y+8, 37.5, 37.5)];
        myImgaeView.layer.cornerRadius = myImgaeView.frame.size.width/2;
        [myImgaeView.layer setMasksToBounds:YES];
        [self.contentView addSubview:myImgaeView];
        
        myLabelBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"对话框-2.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:30]];
        [self.contentView addSubview:myLabelBg];
        
        myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [myLabel setBackgroundColor:[UIColor clearColor]];
        [myLabel setNumberOfLines:0];
        [myLabel setTextColor:[z_UI colorWithHexString:@"6c6668"]];
        [myLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [self.contentView addSubview:myLabel];
        
        //客服
        serviceImgaeView = [[UIImageView alloc] initWithFrame:myImgaeView.frame];
        serviceImgaeView.layer.cornerRadius = serviceImgaeView.frame.size.width/2;
        [serviceImgaeView.layer setMasksToBounds:YES];
        [self.contentView addSubview:serviceImgaeView];
        
        answerLabelBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"对话框-1.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:30]];
        [self.contentView addSubview:answerLabelBg];
        
        answerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [answerLabel setBackgroundColor:[UIColor clearColor]];
        [answerLabel setTextColor:[z_UI colorWithHexString:@"838383"]];
        [answerLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [answerLabel setNumberOfLines:0];
        [self.contentView addSubview:answerLabel];
        
    }
    return self;
}

/**
 *  设置内容
 *
 *  @param info    用户信息
 *  @param myImage 用户头像
 */
-(void)setContent:(ConsultInfo*)info myImage:(NSString*)myImage
{
    [timeLabel setHidden:YES];
    [myImgaeView setHidden:YES];
    [myLabel setHidden:YES];
    [myLabelBg setHidden:YES];
    [serviceImgaeView setHidden:YES];
    [answerLabel setHidden:YES];
    [answerLabelBg setHidden:YES];
    
    //设置时间
    if (info.pubtime) {
        [timeLabel setText:info.pubtime];
        [timeLabel setHidden:NO];
    }
    
    if ([info.isreply intValue] == 0) {
        
        //设置头像信息
        [myImgaeView setImageWithURL:[NSURL URLWithString:myImage] placeholderImage:[UIImage imageNamed:MR_TX_NAME]];
        
        [myImgaeView setHidden:NO];
        [myLabel setHidden:NO];
        [myLabelBg setHidden:NO];
        
        CGFloat y = 12;
        if (info.pubtime) {
            y = timeLabel.frame.size.height+timeLabel.frame.origin.y+8;
        }
        
        CGRect rect = myImgaeView.frame;
        rect.origin.y= y;
        [myImgaeView setFrame:rect];
        [myLabel setText:info.content];
        
        if (info.myTextBgRect.origin.x == 0) {
            //没有设置CGRect
            CGSize mySize = [z_UI returnHeightFloat:info.content frontSize:[UIFont boldSystemFontOfSize:14] frontWidth:160];
            CGRect myTextRect = CGRectMake(myImgaeView.frame.origin.x-30-mySize.width, 12+myImgaeView.frame.origin.y, mySize.width, mySize.height);
            [myLabel setFrame:myTextRect];
            info.myTextRect = myTextRect;
            
            CGRect myTextBgRect = CGRectMake(myLabel.frame.origin.x-ChatRightGap, myLabel.frame.origin.y-ChatYGap, myLabel.frame.size.width+ChatLeftGap+ChatRightGap, myLabel.frame.size.height+ChatYGap*2);
            [myLabelBg setFrame:myTextBgRect];
            info.myTextBgRect = myTextBgRect;
        }else{
            [myLabel setFrame:info.myTextRect];
            [myLabelBg setFrame:info.myTextBgRect];
        }
    }else{
        
        //设置头像信息
        NSString *headImage = [info.user valueForKeyPath:@"headimg"];
        [serviceImgaeView setImageWithURL:[NSURL URLWithString:headImage] placeholderImage:[UIImage imageNamed:MR_TX_NAME]];
        
        [serviceImgaeView setHidden:NO];
        [answerLabel setHidden:NO];
        [answerLabelBg setHidden:NO];
        
        CGFloat y = 12;
        if (info.pubtime) {
            y = timeLabel.frame.size.height+timeLabel.frame.origin.y+8;
        }
        
//        [serviceImgaeView setFrame:CGRectMake(14, y, 37.5, 37.5)];
        [serviceImgaeView setCenter:CGPointMake(14+serviceImgaeView.frame.size.width/2, y+serviceImgaeView.frame.size.height/2)];
        [answerLabel setText:info.content];
        
        if (info.serviceTextBgRect.origin.x == 0) {
            //没有设置CGRect
            CGSize serviceSize = [z_UI returnHeightFloat:info.content frontSize:[UIFont boldSystemFontOfSize:14] frontWidth:160];
            CGRect answerLabelRect = CGRectMake(serviceImgaeView.frame.origin.x+serviceImgaeView.frame.size.width+30, 12+serviceImgaeView.frame.origin.y, serviceSize.width, serviceSize.height);
            [answerLabel setFrame:answerLabelRect];
            info.serviceTextRect = answerLabelRect;
            
            CGRect answerLabelBgRect = CGRectMake(answerLabel.frame.origin.x-ChatLeftGap, answerLabel.frame.origin.y-ChatYGap, answerLabel.frame.size.width+ChatLeftGap+ChatRightGap, answerLabel.frame.size.height+ChatYGap*2);
            [answerLabelBg setFrame:answerLabelBgRect];
            info.serviceTextBgRect = answerLabelBgRect;
        }else{
            [answerLabel setFrame:info.serviceTextRect];
            [answerLabelBg setFrame:info.serviceTextBgRect];
        }
    }
}

/**
 *  设置新消息的内容
 *
 *  @param info    用户信息
 *  @param myImage 用户头像
 */
-(void)setNewMSGContent:(ConsultInfo*)info myImage:(NSString*)myImage
{
    
}

@end
